package com.study.file.dao;

import com.study.common.BaseDao.BaseDaoImpl;
import com.study.common.entity.LoginException;
import com.study.common.entity.User;
import com.study.common.enumerate.ExceptionInfo;
import com.study.main.vo.ConditionPagination;
import com.study.main.vo.UserInfo;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @Author 谭永超 [www.tanyongchao.tk:8090/SumDome/index]
 * @Date 2018/9/10 15:04
 * @Description
 */
@Repository(value = "fileDao")
public class FileDaoImpl extends BaseDaoImpl implements FileDao {
    @Override
    public User findByAll(User user) {
        Session session = getSession();
        String hql = "from User where  userName = ? and telNumber= ? ";
        Query query = session.createQuery(hql);
        query.setParameter(1, user.getTelNumber());
        query.setParameter(0, user.getUserName());
        return (User)query.uniqueResult();
    }

    @Override
    public List<UserInfo> selectAll(ConditionPagination cp) {
        List<UserInfo> result = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("chinese_name",cp.getChineseName());
        map.put("telNumber",cp.getTelNumber());
        map.put("gitlabNumber",cp.getGitlabNumber());
        map.put("email",cp.getEmail());
        map.put("credit",cp.getCredit());
        map.put("grade",cp.getGrade());
        Session session = getSession();
        String hql = "SELECT new com.study.main.vo.UserInfo(i.userId,i.infoId,u.chineseName,u.userName,u.telNumber,u.gitlabNumber,u.email,i.credit,i.grade)" +
                "from User as u,Information as i where u.userId = i.userId and";
        try {
            hql += " 1=1 and";
            Session s = getSession();
            if (!map.isEmpty()) {
                Iterator<String> it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    if (map.get(key) != null && !map.get(key).equals("")) {
                        String a = (String)map.get(key);
                        if(!a.contains("%")){
                        hql = hql + " " + key + " like :"+key+"  and ";
                        }else{
                            hql = hql + " 1=2 and ";
                        }
                    }
                }
            }
            hql += " 2=2";
            System.out.println(hql);
            Query query = s.createQuery(hql);
            int i = 0;
            if (!map.isEmpty()) {
                Iterator<String> it = map.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next();
                    if (map.get(key) != null && !map.get(key).equals("")) {
                        String a = (String)map.get(key);
                        if(!a.contains("%")){
                        query.setString(key,"%"+map.get(key)+"%");
                        i++;
                        }
                    }
                }
            }
            result = query.list();
        } catch (HibernateException e) {
            throw new LoginException(ExceptionInfo.SELECT_ERROR);
        }
        return result;
    }

    @Override
    public UserInfo findUserInfoById(int infoId) {
        Session session = getSession();
        String hql = "SELECT new com.study.main.vo.UserInfo(i.userId,i.infoId,u.chineseName,u.userName,u.telNumber,u.gitlabNumber,u.email,i.credit,i.grade)" +
                "from User as u,Information as i where u.userId = i.userId and i.infoId = ?";
        Query query = session.createQuery(hql);
        query.setParameter(0,infoId);
        return (UserInfo) query.uniqueResult();
    }
}
